package com.hluobo.gnnulibrary.view;

import com.hluobo.gnnulibrary.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class TipTextView extends View {

	private static final String TAG = "com.hluobo.gnnulibrary";

	private Paint mPaint;
	private Paint bgPaint;
	private String mText = "0";
	private int textColor = Color.WHITE;
	private int circleColr = Color.BLUE;

	private int textPadding = 5;

	private float mRadius = 30.0f;

	private float mTextWidth = 0;
	private float mTextHeight = 0;

	public TipTextView(Context context) {
		super(context);
		initpaint();
	}

	public TipTextView(Context context, AttributeSet attrs) {
		super(context, attrs);

		initializeCustomAttributes(attrs);
		
		initpaint();
	}

	private final void initpaint() {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
		mPaint.setColor(textColor);
		setPadding(3, 3, 3, 3);

		bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		bgPaint.setColor(circleColr);

	}

	private void initializeCustomAttributes(AttributeSet attrs) {
		
		TypedArray attributeArray = getContext().obtainStyledAttributes(attrs,
				R.styleable.TipTextView);

		mText = attributeArray.getString(R.styleable.TipTextView_text);
		if (mText == null) {
			mText = "0";
		}

		textColor = attributeArray.getColor(R.styleable.TipTextView_textColor,
				Color.WHITE);

		circleColr = attributeArray.getColor(
				R.styleable.TipTextView_circleColr, Color.BLUE);
		
		//setCircleColr(ircleColr);
		
		//Log.i(TAG, "circleColr = "+circleColr+" | "+ircleColr);
		
//		textPadding = attributeArray.getInt(
//				R.styleable.TipTextView_textPadding, 5);
		
		textPadding = attributeArray.getDimensionPixelSize(R.styleable.TipTextView_textPadding, 5);

		attributeArray.recycle();
		
	}

	public void setText(String text) {
		mText = text;
		requestLayout();
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		int viewWeight = getWidth();
		int viewHeight = getHeight();

		Log.w(TAG, "viewWeight = " + viewWeight + ";viewHeight = " + viewHeight);

		// canvas.translate(viewWeight/2, viewHeight/2);

		// mRadius = calculateCircleRadiu(mTextWidth, mTextHeight) +
		// textPadding;

		// Log.e(TAG, "绘画时的的 radius = "+mRadius);

		float cx = getPaddingLeft() + mRadius;
		float cy = getPaddingTop() + mRadius;
		// float radius = mTextWidth/2.0f + textPaddingCircle;
		canvas.drawCircle(cx, cy, mRadius, bgPaint);

		float tx = cx - mTextWidth / 2;
		float ty = cy - mTextHeight / 2;
		//
		canvas.drawText(mText, tx, ty, mPaint);
	}

	private float calculateCircleRadiu(float mTextWidth, float mTextHeight) {
		return (float) Math.sqrt(Math.pow(mTextWidth / 2, 2)
				+ Math.pow(mTextHeight / 2, 2));
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}

	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		Log.w(TAG, "Width specMode --> " + getModeString(specMode));
		mTextWidth = mPaint.measureText(mText);

		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be
			result = specSize;
		} else {
			// Measure the text
			// result = (int) mPaint.measureText(mText) + getPaddingLeft()
			// + getPaddingRight() + textPaddingCircle;

			// Log.e(TAG, "测量width的 radius = "+mRadius);

			result = (int) (getPaddingLeft() + (2 * mRadius) + getPaddingRight());
			if (specMode == MeasureSpec.AT_MOST) {
				// Respect AT_MOST value if that was what is called for by
				// measureSpec
				result = Math.min(result, specSize);
			}
		}

		if (result == mTextWidth) {
			Log.e(TAG, "measureWidth true");
		}

		return result;
	}

	private int measureHeight(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		Log.w(TAG, "Height specMode --> " + getModeString(specMode));

		mTextHeight = mPaint.descent() + mPaint.ascent();

		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be
			result = specSize;
		} else {
			// Measure the text (beware: ascent is a negative number)
			// result = (int) (-mAscent + mPaint.descent()) + getPaddingTop()
			// + getPaddingBottom() + textPaddingCircle;
			mRadius = calculateCircleRadiu(mTextWidth, mTextHeight)
					+ textPadding;

			// Log.e(TAG, "测量height的 radius = "+mRadius);
			result = (int) (getPaddingTop() + (2 * mRadius) + getPaddingBottom());

			if (specMode == MeasureSpec.AT_MOST) {
				// Respect AT_MOST value if that was what is called for by
				// measureSpec
				result = Math.min(result, specSize);
			}
		}

		if (result == mTextHeight) {
			Log.e(TAG, "measureHeight true");
		}

		return result;
	}

	private String getModeString(int mode) {
		String modeStr = null;

		switch (mode) {
		case MeasureSpec.AT_MOST:
			modeStr = "AT_MOST";
			break;
		case MeasureSpec.EXACTLY:
			modeStr = "EXACTLY";
			break;
		case MeasureSpec.UNSPECIFIED:
			modeStr = "UNSPECIFIED";
			break;
		}

		return modeStr;
	}

	public int getTextColor() {
		return textColor;
	}

	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}

	public int getCircleColr() {
		return circleColr;
	}

	public void setCircleColr(int circleColr) {
		this.circleColr = circleColr;
	}

	
	
}
